import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { User } from '@/types/user'

// 定义 Store
export const useMemberStore = defineStore(
  'user',
  () => {
    // 用户信息
    const profile = ref<User>()
    // 保存用户信息，登录时使用
    const setProfile = (val: User) => {
      profile.value = val
      console.log(profile.value)
    }
    //修改用户头像和昵称
    const changeUserInfo = (nickname: string, picture: string) => {
      profile.value!.nickname = nickname
      profile.value!.picture = picture
    }
    // 清理用户信息，退出时使用
    const clearProfile = () => {
      profile.value = undefined
    }

    // 记得 return
    return {
      profile,
      setProfile,
      clearProfile,
      changeUserInfo,
    }
  },
  // TODO: 持久化
  {
    persist: {
      storage: {
        getItem: (key) => uni.getStorageSync(key),
        setItem: (key, value) => uni.setStorageSync(key, value),
      },
    },
  },
)
